package com.tinama.jedis01.demo;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lgc
 * @createDate 2020/10/9 - 17:05
 */
public class Test {
    public static void main(String[] args){
        Tiket tiket = new Tiket();
        new Thread(()->{for (int i=1; i<30; i++)tiket.sale();},"A").start();
        new Thread(()->{for (int i=1; i<30; i++)tiket.sale();},"B").start();
        new Thread(()->{for (int i=1; i<30; i++)tiket.sale();},"C").start();
    }
     static class Tiket{
        private int number = 30;
        Lock lock  = new ReentrantLock();
        public void sale(){
            lock.lock();
            try {
                if (number>0){
                    System.out.println(Thread.currentThread().getName()+"卖了第"+number--+"张票/t还剩余"+number+"张");
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }
}
